package com.vm.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.db.LocalUserInfoDao;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class SetInvCodeActivity extends Activity {

	Button btn_sure;
	Button btn_get_invite_code;
	EditText et_invCode;
	TextView tv_msg_invCode;
	RequestQueue mQueue;
	ImageButton imgbtn_search_back;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.invite_code);
		init();

		mQueue = MyVolley.getRequestQueue();
	}

	public void init() {
		btn_sure = (Button) findViewById(R.id.btn_sure);
		btn_get_invite_code = (Button) findViewById(R.id.btn_get_invite_code);
		et_invCode = (EditText) findViewById(R.id.et_invCode);
		tv_msg_invCode = (TextView) findViewById(R.id.tv_msg_invCode);

		btn_get_invite_code.setOnClickListener(btnClickListener);
		btn_sure.setOnClickListener(btnClickListener);
		
		imgbtn_search_back = (ImageButton) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
	}

	OnClickListener btnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String url = null;

			StringRequest stringRequest = null;
			switch (v.getId()) {
			case R.id.btn_get_invite_code:
				// UIHelper.showDialogForLoading(SetInvCodeActivity.this,
				// "正在获取...", true);

				btn_get_invite_code.setEnabled(false);

				url = VMConst.REQUEST_URL + "getRanodmInvCode";

				stringRequest = new StringRequest(url, new Listener<String>() {

					@Override
					public void onResponse(String arg0) {
						// TODO Auto-generated method stub
						Message msg = new Message();
						msg.what = 100;
						msg.obj = arg0;
						handler.sendMessage(msg);
					}

				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError arg0) {
						// TODO Auto-generated method stub

					}
				});
				mQueue.add(stringRequest);

				break;
			case R.id.btn_sure:
				String invCode = et_invCode.getText().toString();
				if (!isLetterDigitOrChinese(invCode)) {
					tv_msg_invCode.setVisibility(View.VISIBLE);
					tv_msg_invCode.setText("邀请码只能由数字和字母组成");
				} else if (invCode.length() >= 6 && invCode.length() <= 10) {

					// UIHelper.showDialogForLoading(SetInvCodeActivity.this,
					// "正在验证...", true);
					btn_get_invite_code.setEnabled(false);

					url = VMConst.REQUEST_URL + "saveInvCode?invCode="
							+ invCode + "&userId=" + CurrentUser.USERID;
					stringRequest = new StringRequest(url,
							new Listener<String>() {

								@Override
								public void onResponse(String arg0) {
									// TODO Auto-generated method stub
									Message msg = new Message();

									if (arg0.contains("成功")) {
										msg.what = 300;
									} else {
										msg.what = 200;
									}
									handler.sendMessage(msg);

								}

							}, new Response.ErrorListener() {

								@Override
								public void onErrorResponse(VolleyError arg0) {
									// TODO Auto-generated method stub

								}
							});
					mQueue.add(stringRequest);
				} else {
					tv_msg_invCode.setVisibility(View.VISIBLE);
					tv_msg_invCode.setText("邀请码的位数必须为6-10位");
				}

				break;
			default:
				break;
			}

		}
	};

	public static boolean isLetterDigitOrChinese(String str) {
		Pattern p = Pattern.compile("[A-Z,a-z,0-9,-]*");
		Matcher m = p.matcher(str);
		if (m.matches()) {
			return true;
		} else {
			return false;
		}
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {// handler接收到消息后就会执行此方法
			// pd.dismiss();// 关闭ProgressDialog
			// UIHelper.hideDialogForLoading();
			switch (msg.what) {

			case 100:
				btn_get_invite_code.setEnabled(true);

				et_invCode.setText(msg.obj.toString().replace("\"", ""));
				break;
			case 200:
				tv_msg_invCode.setVisibility(View.VISIBLE);
				tv_msg_invCode.setText("邀请码已存在");
				break;
			case 300:
				tv_msg_invCode.setVisibility(View.VISIBLE);
				tv_msg_invCode.setText("邀请码可用");

				CurrentUser.INVCODE = et_invCode.getText().toString();
				LocalUserInfoDao luidao = new LocalUserInfoDao(
						SetInvCodeActivity.this);
				luidao.updateInvCode();
				SetInvCodeActivity.this.finish();
				onBackPressed();
				break;
			default:
				break;
			}

		}

	};
	

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

}
